package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.GoodsDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.GoodsCommand;
import com.cskaoyan.wordmemorize.request.GoodsPageRequest;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.GoodsService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

// todo 【后台管理-商品相关功能】
@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    GoodsService goodsService;

    // todo 添加商品
    @SaAdminCheckPermission("admin:goods:add")
    @PostMapping("")
    public Result addGood(@RequestBody GoodsCommand goodsCommand){
        goodsService.addGoods(goodsCommand);
        return Result.ok();
    }

    // todo 修改商品
    @SaAdminCheckPermission("admin:goods:update")
    @PutMapping("")
    public Result updateGood(@RequestBody GoodsCommand goodsCommand){
        goodsService.updateGoods(goodsCommand);
        return Result.ok();
    }

    // todo 修改商品状态
    @PutMapping("/status/{goodsId}")
    public Result updateGoodsStatus(@PathVariable Long goodsId, @RequestParam Integer status){
        goodsService.updateGoodsStatus(goodsId, status);
        return Result.ok();
    }

    // todo 获取商品的分页列表
    @GetMapping("/list")
    public Result<PageDTO<GoodsDTO>> getGoodsPage(GoodsPageRequest goodsPageRequest){
        PageDTO<GoodsDTO> goodsPage = goodsService.getGoodsPage(goodsPageRequest);
        return Result.ok(goodsPage);
    }

    // todo 删除商品
    @SaAdminCheckPermission("admin:goods:delete")
    @DeleteMapping("/{goodsId}")
    public Result deleteGoods(@PathVariable Long goodsId){
        goodsService.deleteGoods(goodsId);
        return Result.ok();
    }

    // todo 删除选中商品
    @SaAdminCheckPermission("admin:goods:batchdelete")
    @DeleteMapping("/selected")
    public Result deleteGoodsSelected(@RequestBody BatchDeleteCommand batchDeleteCommand){
        goodsService.deleteGoodsSelected(batchDeleteCommand);
        return Result.ok();
    }
}
